﻿using DeviceMaintainGIS.Entity;
using DeviceMaintainGIS.Model;
using DeviceMaintainGIS.Model.DevInfo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceMaintainGIS.BLL.Dev
{
    /// <summary>
    /// 设备操作
    /// </summary>
    public class DevService
    {
        /// <summary>
        /// 根据条件查询设备信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ReturnListVM<DevInfoModel> GetDevList(DevRequest request)
        {
            ReturnListVM<DevInfoModel> list = new ReturnListVM<DevInfoModel>();
            using (CYOADBEntities Entities = new CYOADBEntities())
            {


                var linq = from a in Entities.DevInfo_Tab//.Where(i => i.DeviceLevel == 1)
                           join f in Entities.DevType_Tab on a.DeviceTypeId equals f.DeviceTypeId
                            into tmpLocaType
                           let k = tmpLocaType.FirstOrDefault()
                           select new DevInfoModel
                           {
                               ID = a.ID,
                               DeviceId = a.DeviceId,
                               DeviceLevel = a.DeviceLevel,
                               DeviceName = a.DeviceName,
                               DeviceTypeId = a.DeviceTypeId,
                               DeviceTypeName = k == null ? "" : k.DeviceName,
                               FactorySerial = a.FactorySerial,
                               CompanySerial = a.CompanySerial,
                               FModelSize = a.FModelSize,
                               CModelSize = a.CModelSize,
                               Purchaser = a.Purchaser,
                               PurchaseTime = a.PurchaseTime,
                               WarrantyTime = a.WarrantyTime,
                               DeviceFactory = a.DeviceFactory,
                               DeviceTel = a.DeviceTel,
                               State = a.State,
                               RepairFactoryManager = a.RepairFactoryManager,
                               AddTime = a.AddTime

                           };
                //模糊查询
                if (!string.IsNullOrEmpty(request.Indistinct))
                {
                    linq = linq.Where(i => i.DeviceName.Contains(request.Indistinct) || i.CompanySerial.Contains(request.Indistinct) || i.CModelSize.Contains(request.Indistinct));
                }
                //查询设备等级是一级并且已出库的设备
                linq = linq.Where(i => i.DeviceLevel == 1 && i.State == 1);
                ////设备名称
                //if (!string.IsNullOrEmpty(request.DeviceName))
                //{
                //    linq = linq.Where(i => i.DeviceName.Contains(request.DeviceName));
                //}
                ////公司序列号
                //if (!string.IsNullOrEmpty(request.CompanySerial))
                //{
                //    linq = linq.Where(i => i.CompanySerial.Contains(request.CompanySerial));
                //}
                ////公司规格型号
                //if (!string.IsNullOrEmpty(request.CModelSize))
                //{
                //    linq = linq.Where(i => i.CModelSize.Contains(request.CModelSize));
                //}
                DateTime tmp = DateTime.ParseExact("19000101", "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
                //质保期
                if (request.selStartWarrantyTime != tmp && request.selEndWarrantyTime != tmp)
                {
                    linq = linq.Where(a => a.WarrantyTime >= request.selStartWarrantyTime && a.WarrantyTime <= request.selEndWarrantyTime);
                }
                list.TotalCount = linq.Count();
                list.List = linq.OrderBy(i => i.ID).Take(request.PagesSize * request.PageIndex).Skip(request.PagesSize * (request.PageIndex - 1)).ToList();
                foreach (DevInfoModel diem in list.List)
                {
                    int Documentint = Entities.DocumentRelation_Tab.Where(i => i.Type == 3 && i.LPDId == diem.DeviceId).Count();
                    if (Documentint > 0)
                    {
                        diem.IsDocument = true;
                    }
                    else
                    {
                        diem.IsDocument = false;
                    }
                    int Proint = Entities.DevProRelation_Tab.Where(i => i.DeviceId == diem.DeviceId && i.CodeId == "1").Count();
                    if (Proint > 0)
                    {
                        diem.IsPro = true;
                    }
                    else
                    {
                        diem.IsPro = false;
                    }
                    int Locaint = Entities.DevProRelation_Tab.Where(i => i.DeviceId == diem.DeviceId && i.LocaId != null).Count();
                    if (Locaint > 0)
                    {
                        diem.IsLoca = true;
                    }
                    else
                    {
                        diem.IsLoca = false;
                    }
                }
                list.PageIndex = request.PageIndex;
                list.PagesSize = request.PagesSize;


            };
            return list;
        }
        /// <summary>
        /// 根据项目 基站查询设备
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ReturnListVM<DevInfoExtendModel> GetDevExtendList(DevRequest request)
        {
            ReturnListVM<DevInfoExtendModel> list = new ReturnListVM<DevInfoExtendModel>();
            using (CYOADBEntities Entities = new CYOADBEntities())
            {
                var linq = from a in Entities.DevProRelation_Tab
                           join f in Entities.DevInfo_Tab on a.DeviceId equals f.DeviceId
                            into tmpDevInfo
                           let DevInfoTab = tmpDevInfo.FirstOrDefault()
                           join f in Entities.DevType_Tab on DevInfoTab.DeviceTypeId equals f.DeviceTypeId
                            into tmpLocaType
                           let k = tmpLocaType.FirstOrDefault()
                           select new DevInfoExtendModel
                           {
                               ID = DevInfoTab.ID,
                               DeviceId = a.DeviceId,
                               DeviceLevel = DevInfoTab.DeviceLevel,
                               DeviceName = DevInfoTab.DeviceName,
                               DeviceTypeId = DevInfoTab.DeviceTypeId,
                               DeviceTypeName = k == null ? "" : k.DeviceName,
                               FactorySerial = DevInfoTab.FactorySerial,
                               CompanySerial = DevInfoTab.CompanySerial,
                               FModelSize = DevInfoTab.FModelSize,
                               CModelSize = DevInfoTab.CModelSize,
                               Purchaser = DevInfoTab.Purchaser,
                               PurchaseTime = DevInfoTab.PurchaseTime,
                               WarrantyTime = a.WarrantyTime,
                               DeviceFactory = DevInfoTab.DeviceFactory,
                               DeviceTel = DevInfoTab.DeviceTel,
                               State = DevInfoTab.State,
                               RepairFactoryManager = DevInfoTab.RepairFactoryManager,
                               AddTime = a.AddTime,
                               Code = a.Code,
                               CodeId = a.CodeId,
                               LocaId = a.LocaId

                           };
                //模糊查询
                if (!string.IsNullOrEmpty(request.Indistinct))
                {
                    linq = linq.Where(i => i.DeviceName.Contains(request.Indistinct) || i.CompanySerial.Contains(request.Indistinct) || i.CModelSize.Contains(request.Indistinct));
                }
                //查询设备等级是一级并且已出库的设备
                linq = linq.Where(i => i.DeviceLevel == 1 && i.State == 1);
                //通过项目编号查询
                if (request.Type == 1)
                {
                    linq = linq.Where(i => i.Code == request.LPDId && i.CodeId == "1");
                }
                //通过基站编号查询
                if (request.Type == 2)
                {
                    linq = linq.Where(i => i.LocaId == request.LPDId);
                }

                list.TotalCount = linq.Count();
                list.List = linq.OrderBy(i => i.ID).Take(request.PagesSize * request.PageIndex).Skip(request.PagesSize * (request.PageIndex - 1)).ToList();





                list.PageIndex = request.PageIndex;
                list.PagesSize = request.PagesSize;
            }
            return list;
        }
    }
}
